package com.ailanyin.common.utils;

import com.ailanyin.common.constant.Constants;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 获取IP工具类
 *
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/2 0003 下午 14:10
 */
public class IpUtil {

    private final static String UNKNOWN = "unknown";
    private final static String CHINESE_UNKNOWN = "未知";
    private final static String X_FORWARDED_FOR_LOWERCASE = "x-forwarded-for";
    private final static String X_FORWARDED_FOR_CAPITALIZE = "X-Forwarded-For";
    private final static String PROXY_CLIENT_IP = "Proxy-Client-IP";
    private final static String WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";
    private final static String X_REAL_IP = "X-Real-IP";
    private final static String LOCALHOST = "127.0.0.1";
    private final static String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";
    public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";

    /**
     * 获取请求的ip地址
     *
     * @param request HttpServletRequest
     * @return 请求对应的ip
     */
    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return UNKNOWN;
        }
        String ip = request.getHeader(X_FORWARDED_FOR_LOWERCASE);
        if (EmptyUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader(PROXY_CLIENT_IP);
        }
        if (EmptyUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader(X_FORWARDED_FOR_CAPITALIZE);
        }
        if (EmptyUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader(WL_PROXY_CLIENT_IP);
        }
        if (EmptyUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader(X_REAL_IP);
        }

        if (EmptyUtil.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return LOCALHOST_IPV6.equals(ip) ? LOCALHOST : ip;
    }

    public static String getHostIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException ignored) {
        }
        return LOCALHOST;
    }

    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException ignored) {
        }
        return CHINESE_UNKNOWN;
    }

    /**
     * 获取公网ip的实际位置
     *
     * @param ip 公网ip
     * @return eg:安徽省合肥市 电信
     */
    public static String getRealAddressByIp(String ip) {
        // 内网不查询
        if (LOCALHOST.equals(ip)) {
            return "内网IP";
        }
        try {
            String rspStr = HttpUtil.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
            if (EmptyUtil.isEmpty(rspStr)) {
                return UNKNOWN;
            }
            JSONObject obj = JSONObject.parseObject(rspStr);
            return obj.getString("addr");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CHINESE_UNKNOWN;
    }
}
